<template>
  <TopNav title="NSR-2002营养风险评估"/>
  <div class="container">
    <!--顶部 start-->
    <div class="top">
      <!--内部评分 end-->
      <el-progress class="score-in"
                   type="circle"
                   color="#409eff"
                   :percentage="Number(assessment_score)*10"
                   :stroke-width="26"
                   width="200"
      >
        <template #default="{ percentage }">
          <p>{{ assessment_score }}</p>
          <p><span>{{ assessment_result }}</span></p>
        </template>
      </el-progress>
      <!--外部评估进度 end-->
      <el-progress class="score-out"
                   type="circle"
                   color="#3ddadd"
                   :indeterminate="true"
                   :percentage="process"
                   :stroke-width="10"
                   width="200"
      >
        <template #default="{ percentage }">
          <p></p>
        </template>
      </el-progress>
    </div>
    <!--顶部 end-->
    <!--按钮 start-->
    <el-button v-if="process<20" @click="assessmentHandle" class="button" round color="#3ddadd"><span>开始评估</span>
    </el-button>
    <el-button v-else-if="process<100" class="button" round color="#3ddadd"><span>正在评估中...</span></el-button>
    <span v-else>
      <el-button class="button" round color="#3ddadd"><span>评估完成</span></el-button>
      <el-button @click="router.push('/plan/createPlan/'+route.params.name)" class="button" round color="#409eff"><span>生成个性化的营养治疗方案</span></el-button>
    </span>
    <!--按钮 end-->
    <!--分析 start-->
    <el-card class="info" v-if="assessment_info!=''">
      <p>{{ assessment_info }}</p>
    </el-card>
    <!--分析 end-->
  </div>
</template>

<script setup>

import {useDataStore} from "@/stores/dataStore.js";
import {useRoute, useRouter} from 'vue-router'
import TopNav from "@/components/TopNav/Back.vue"
import {onMounted, ref} from "vue";
import patientAxios from "@/api/patient.js"
import langchain4jAxios from "@/api2/langchain4j.js"

const route = useRoute()
const router = useRouter()
const dataStore = useDataStore()
//评估进度
const process = ref(0)
//评估分析、分数、结果
const assessment_info = ref('')
const assessment_score = ref(0)
const assessment_result = ref("")
onMounted(() => {
})
/**
 * 评估
 */
const assessmentHandle = () => {
  console.log("1.开始评估....")
  process.value = 20
  //5秒后执行
  new Promise(resolve => setTimeout(http_assessment, 5000));
}
/**
 * 评估请求【外置ai版】
 */
const patient_text = ref('')
const http_assessment = () => {
  console.log("2.发送评估请求....")
  process.value = 40
  http_patient_findById()
}
//1.查询患者，获取患者信息，制作ai提示词
const http_patient_findById = () => {
  patientAxios.patient_findById({
    patientId: route.params.name
  }).then(res => {
    if (res.data.code == 200) {
      console.log("3.患者信息已获取....")
      process.value = 60
      patient_text.value = "根据以下患者信息"
          + "，患者的性别为：" + res.data.data.patientSex
          + "，患者的年龄为：" + res.data.data.patientAge
          + "，患者的身高为：" + res.data.data.height
          + "，患者的体重为：" + res.data.data.weight
          + "，患者所患疾病为：" + res.data.data.disease
      http_ai_chat()
    } else {
      ElMessage.error(res.data.data.message)
    }
  })
}
//2.访问ai，获取分析过程、评分、风险结果
const http_ai_chat = () => {
  langchain4jAxios.base_patientNRS({
    message: patient_text.value
  }).then(res => {
    if (res.data.code == 200) {
      console.log("4.大模型解析出分析过程、评分、风险结果等信息....")
      process.value = 80
      assessment_info.value = res.data.data.assessmentProcess
      assessment_score.value = res.data.data.patientNRS
      assessment_result.value = res.data.data.assessmentResult
      http_patient_nrs2002AssessmentWebAi()
    }
  })
}
//3.保存评分和结果到数据库
const http_patient_nrs2002AssessmentWebAi = () => {
  patientAxios.patient_nrs2002AssessmentWebAi({
    patientId: route.params.name,
    totalScore: assessment_score.value,
    assessmentResult: assessment_result.value
  }).then(res => {
    if (res.data.code == 200) {
      console.log("5.大模型解析的信息保存到数据库....")
      process.value = 100
    }
  })
}
</script>

<style scoped lang="scss">
.container {
}

.top {
  background-color: #3ddadd;
  border-radius: 0 0 30% 30%;
  height: 300px;

  .score-in {
    position: fixed;
    margin-top: 30px;
    margin-left: 25%;

    p {
      font-size: 30px;
      color: #409eff;

      span {
        font-size: 15px;
        font-family: 楷体;
        color: #de4848;
      }
    }
  }

  .score-out {
    position: fixed;
    margin-top: 30px;
    margin-left: 25%;
  }
}

.button {
  margin-top: 20px;
  margin-left: 10%;
  width: 80%;
  height: 45px;

  span {
    font-weight: bolder;
    font-size: 18px;
    color: #ffffff;
  }
}

.info {
  margin: 20px;

  p {
    line-height: 27px;
  }

}
</style>
